﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VSA.Applications
{
    public partial class VisitCardInfoForm : Form
    {
        public Int32 PersonId { get; set; }
        public string FaceFileName { get; set; }
        public string BackFileName { get; set; }
        public PersonInfoForm ContactPersonForm { get; set; }

        public VisitCardInfoForm()
        {
            InitializeComponent();
        }

        public VisitCardInfoForm(String facePicName, String backPicName)
        {
            InitializeComponent();

            if (String.IsNullOrEmpty(facePicName))
            {
                this.btnDeleteFace.Enabled = false;
            }
            if (String.IsNullOrEmpty(backPicName))
            {
                this.btnDeleteBack.Enabled = false;
            }
        }

        private void btnDeleteFace_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure delete this file？", "Delete confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Boolean bDelete = WebServiceFactory.GetService().DeleteVisitCard(this.PersonId, this.FaceFileName, 0);
                if (bDelete)
                {
                    MessageBox.Show("Visicard face file delete success.");
                    this.btnDeleteFace.Enabled = false;
                    this.FaceFileName = String.Empty;
                    this.ContactPersonForm.AttachVisitCard(this.FaceFileName, this.BackFileName);
                }
                else
                {
                    MessageBox.Show("Visicard face file delete failed.");
                }
            }
        }

        private void btnDeleteBack_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure delete this file？", "Delete confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Boolean bDelete = WebServiceFactory.GetService().DeleteVisitCard(this.PersonId, this.BackFileName, 1);
                if (bDelete)
                {
                    MessageBox.Show("Visicard back file delete success.");
                    this.btnDeleteBack.Enabled = false;
                    this.BackFileName = String.Empty;
                    this.ContactPersonForm.AttachVisitCard(this.FaceFileName, this.BackFileName);
                }
                else
                {
                    MessageBox.Show("Visicard back file delete failed.");
                }
            }
        }

        private void btnUploadFace_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.FaceFileName = Path.GetFileName(dialog.FileName);
                FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                byte[] fileData = r.ReadBytes((int)fs.Length);

                Boolean bUpload = false;
                if (fileData != null && fileData.Length > 0)
                {
                    bUpload = WebServiceFactory.GetService().UploadVisitCard(this.PersonId, fileData, this.FaceFileName, 0);
                    fileData = null;
                }
                if (bUpload)
                {
                    MessageBox.Show("Visicard face file upload success.");
                    this.btnDeleteFace.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Visicard face file upload failed.");
                }
                this.ContactPersonForm.AttachVisitCard(this.FaceFileName, this.BackFileName);
            }
        }

        private void btnUploadBack_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.BackFileName = Path.GetFileName(dialog.FileName);
                FileStream fs = new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                byte[] fileData = r.ReadBytes((int)fs.Length);

                Boolean bUpload = false;
                if (fileData != null && fileData.Length > 0)
                {
                    bUpload = WebServiceFactory.GetService().UploadVisitCard(this.PersonId, fileData, this.BackFileName, 1);
                    fileData = null;
                }
                if (bUpload)
                {
                    MessageBox.Show("Visicard back file upload success.");
                    this.btnDeleteBack.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Visicard back file upload failed.");
                }

                this.ContactPersonForm.AttachVisitCard(this.FaceFileName, this.BackFileName);
            }
        }
    }
}
